package cn.daxi.mybatisplus;

import com.baomidou.mybatisplus.enums.FieldFill;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.po.TableFill;
import com.baomidou.mybatisplus.generator.config.rules.DbType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import org.springframework.core.io.support.PropertiesLoaderUtils;
import org.springframework.util.StringUtils;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

/**
 * 基于Mybatis-Plus的代码生产工具
 * @author daxi
 * @version 1.0
 */
public class MybatisPlusGenerator {
    public static void main(String[] args) throws IOException {
        new MybatisPlusGenerator().run();
    }

    public void run() throws IOException {
        // 加载配置文件
        Properties properties = PropertiesLoaderUtils.loadAllProperties("application.properties");
        AutoGenerator mpg = new AutoGenerator();

        // ↓↓↓↓全局配置↓↓↓↓
        GlobalConfig gc = new GlobalConfig();
        gc.setOutputDir(properties.getProperty("mybatis.output"));
        gc.setFileOverride(true);
        gc.setActiveRecord(true);
        gc.setEnableCache(false);
        gc.setBaseResultMap(true);// XML ResultMap
        gc.setBaseColumnList(true);// XML columList
        gc.setAuthor(properties.getProperty("mybatis.auth"));

        // 自定义文件命名，注意 %s 会自动填充表实体属性！
        gc.setMapperName("%sMapper");
        gc.setXmlName("%sMapper");
        gc.setServiceName("I%sService");
        gc.setServiceImplName("%sServiceImpl");
        gc.setControllerName("%sController");
        mpg.setGlobalConfig(gc);

        // ↑↑↑↑全局配置↑↑↑↑

        // ↓↓↓↓数据源配置↓↓↓↓
        DataSourceConfig dsc = new DataSourceConfig();
        dsc.setDbType(DbType.valueOf(properties.getProperty("jdbc.db")));
        dsc.setDriverName(properties.getProperty("jdbc.driveName"));
        dsc.setUrl(properties.getProperty("jdbc.url"));
        dsc.setUsername(properties.getProperty("jdbc.username"));
        dsc.setPassword(properties.getProperty("jdbc.password"));
        mpg.setDataSource(dsc);
        // ↑↑↑↑数据源配置↑↑↑↑

        // 策略配置
        StrategyConfig strategy = new StrategyConfig();
        strategy.setNaming(NamingStrategy.underline_to_camel);// 表名生成策略
        if (!StringUtils.isEmpty(properties.getProperty("mybatis.strategy.include"))) {
            strategy.setInclude(properties.getProperty("mybatis.strategy.include").split(",")); // 需要生成的表
        }
        // strategy.setCapitalMode(true);// 全局大写命名 ORACLE 注意
        // strategy.setTablePrefix(new String[] { "tlog_", "tsys_" });// 此处可以修改为您的表前缀
        // strategy.setExclude(new String[]{"test"}); // 排除生成的表

        // 是否是rest controller
        if (!StringUtils.isEmpty(properties.getProperty("mybatis.strategy.isrest"))) {
            strategy.setRestControllerStyle(Boolean.valueOf(properties.getProperty("mybatis.strategy.isrest")));
        }

        // ↓↓↓↓配置集成自定义父类↓↓↓↓
        String controllerSuper = properties.getProperty("mybatis.controller.super");
        if (!StringUtils.isEmpty(controllerSuper)) {
            strategy.setSuperControllerClass(controllerSuper);
        }

        String serviceSuper = properties.getProperty("mybatis.service.super");
        if (!StringUtils.isEmpty(serviceSuper)) {
            strategy.setSuperServiceClass(serviceSuper);
        }

        String serviceImplSuper = properties.getProperty("mybatis.service.impl.super");
        if (!StringUtils.isEmpty(serviceImplSuper)) {
            strategy.setSuperServiceImplClass(serviceImplSuper);
        }

        String entitySuper = properties.getProperty("mybatis.entity.super");
        if (!StringUtils.isEmpty(entitySuper)) {
            strategy.setSuperEntityClass(entitySuper);
        }

        String mapperSuper = properties.getProperty("mybatis.maper.super");
        if (!StringUtils.isEmpty(mapperSuper)) {
            strategy.setSuperMapperClass(mapperSuper);
        }
        // ↑↑↑↑配置集成自定义父类↑↑↑↑

        // 自动填充
        List<TableFill> tableFillList = new ArrayList<TableFill>();
        tableFillList.add(new TableFill("CREATE_DATE", FieldFill.INSERT));
        tableFillList.add(new TableFill("CREATE_BY", FieldFill.INSERT));
        tableFillList.add(new TableFill("UPDATE_DATE", FieldFill.INSERT_UPDATE));
        tableFillList.add(new TableFill("UPDATE_BY", FieldFill.INSERT_UPDATE));
        strategy.setTableFillList(tableFillList);
        mpg.setStrategy(strategy);

        // ↓↓↓↓模板配置↓↓↓↓
        TemplateConfig tc = new TemplateConfig();
        tc.setController("/templates/controller.java.vm");
        tc.setEntity("/templates/entity.java.vm");
        tc.setMapper("/templates/mapper.java.vm");
        tc.setXml("/templates/mapper.xml.vm");
        tc.setService("/templates/service.java.vm");
        tc.setServiceImpl("/templates/serviceImpl.java.vm");
        mpg.setTemplate(tc);
        // ↑↑↑↑模板配置↑↑↑↑

        // ↓↓↓↓包配置↓↓↓↓
        PackageConfig pc = new PackageConfig();
        pc.setParent(properties.getProperty("mybatis.package.name"));
        pc.setModuleName(properties.getProperty("mybatis.module.name"));
        mpg.setPackageInfo(pc);
        // ↑↑↑↑包配置↑↑↑↑

        // 执行生成
        mpg.execute();
    }
}
